import pytest,requests
class Test_A3mall:
    token=None
    spid=None
    # 登录
    def test_login(self):
        url="http://10.59.9.12:7001/api/public/login"
        data={"username":"15727087537","password":"123456"}
        res=requests.post(url=url,json=data)
        print("登录接口",res.json())
        Test_A3mall.token="Bearer "+res.json()["data"]["token"]
        assert res.status_code==200
        assert "钻石会员" in res.text
    # 商品详情
    def test_xq(self):
        url="http://10.59.9.12:7001/api/goods/view?id=32"
        data={"id":32}
        res = requests.get(url=url,json=data)
        print("商品详情",res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 加入购物车
    def test_gwc(self):
        url="http://10.59.9.12:7001/api/cart/add"
        data={"id":32,"sku_id":"","num":1}
        header={"auth-token":Test_A3mall.token,"content-type":"application/json"}
        res=requests.post(url=url,json=data,headers=header)
        print("添加购物车",res.json())
        assert res.status_code == 200
        assert "商品添加至购物车成功" in res.text

    # 查看购物车
    def test_ck(self):
        url="http://10.59.9.12:7001/api/cart?page=1"
        data="page=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, json=data, headers=header)
        print("查看购物车",res.json())
        Test_A3mall.spid = res.json()["data"]["list"][0]["id"]
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 确认订单
    def test_qr(self):
        url=f"http://10.59.9.12:7001/api/order/confirm?id={Test_A3mall.spid}&type=cart&shipping_type=1"
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.get(url=url, headers=header)
        print("确认订单",res.json())
        assert res.status_code == 200
        assert "音乐耳机" in res.text

    # 提交订单
    def test_tj(self):
        url="http://10.59.9.12:7001/api/order/create"
        data={"id":Test_A3mall.spid,"type":"cart","address_id":16,"store_id":1,"shipping_type":1,
               "bonus_id":"0","payment":"balance","remarks":"","source":1,
               "url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=323&type=cart"}
        header = {"auth-token": Test_A3mall.token, "content-type": "application/json"}
        res = requests.post(url=url,json=data,headers=header)
        print("提交订单", res.json())
        assert res.status_code == 200
        assert "支付成功" in res.text